package com.yuantu.judiciary.serviceimpl;

import com.yuantu.judiciary.dao.IUserMessageDao;
import com.yuantu.judiciary.model.domain.UserMessageDO;
import com.yuantu.judiciary.model.enums.MessageType;
import com.yuantu.judiciary.model.vo.MessageInfoVO;
import com.yuantu.judiciary.model.vo.MessageNumVO;
import com.yuantu.judiciary.model.vo.PageVO;
import com.yuantu.judiciary.service.IMessageService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 消息模块
 *
 * @author syw
 * @since 2023-08-23
 **/
@Service
@RequiredArgsConstructor
public class MessageServiceImpl implements IMessageService {

    private final IUserMessageDao userMessageDao;

    @Override
    public Integer getMessageCount(Long accountId) {
        return userMessageDao.countDistinctByCreatorIdAndReadedFalseAndDeletedFalse(accountId);
    }

    @Override
    public List<MessageNumVO> getMessageNum(Long accountId) {
        List<MessageNumVO> res = new ArrayList<>();
        List<UserMessageDO> message = userMessageDao.getUserMessageByCreatorIdAndDeletedFalse(accountId);
        if (CollectionUtils.isEmpty(message)) {
            res.add(new MessageNumVO(MessageType.SYSTEM, 0, "暂无消息"));
            res.add(new MessageNumVO(MessageType.DYNAMIC, 0, "暂无消息"));
            return res;
        }

        Map<MessageType, List<UserMessageDO>> userMsgMap = message.stream()
                .filter(data -> Boolean.FALSE.equals(data.getReaded()))
                .collect(Collectors.groupingBy(UserMessageDO::getType));
        res.add(new MessageNumVO(MessageType.SYSTEM, userMsgMap.containsKey(MessageType.SYSTEM) ? userMsgMap.get(MessageType.SYSTEM).size() : 0,
                !userMsgMap.containsKey(MessageType.SYSTEM) ? "暂无消息"
                        : "您有" + userMsgMap.get(MessageType.SYSTEM).size() + "条系统消息未查收"));
        res.add(new MessageNumVO(MessageType.DYNAMIC, userMsgMap.containsKey(MessageType.DYNAMIC) ? userMsgMap.get(MessageType.DYNAMIC).size() : 0,
                !userMsgMap.containsKey(MessageType.DYNAMIC) ? "暂无消息"
                        : "您关注的企业/个人新增了" + userMsgMap.get(MessageType.DYNAMIC).size() + "条动态"));
        return res;
    }

    @Override
    public PageVO<MessageInfoVO> getListByType(Long accountId, MessageType type, Pageable pageable) {
        Page<UserMessageDO> page = userMessageDao.findUserMessageByCreatorIdAndTypeAndDeletedFalse(accountId, type, pageable);
        return new PageVO<>(page.getTotalElements(), page.getContent().stream().map(data -> {
            MessageInfoVO res = new MessageInfoVO();
            res.setId(data.getId());
            res.setRelativeId(data.getRelativeId());
            res.setTitle(data.getTitle());
            res.setIsRead(data.getReaded());
            res.setCreateTime(data.getCreateTime());
            res.setRemark(data.getRemark());
            return res;
        }).collect(Collectors.toList()));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean read(Long accountId, Long id) {
        userMessageDao.readMessageById(id, accountId, new Timestamp(System.currentTimeMillis()));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean readAllByType(Long accountId, MessageType type) {
        userMessageDao.readAllMessageByType(accountId, type, new Timestamp(System.currentTimeMillis()));
        return true;
    }

}
